package com.yswy.app.service.impl;

import com.alipay.api.response.AlipayTradePayResponse;
import com.auth0.jwt.interfaces.Claim;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.app.service.*;
import com.yswy.common.enums.DateUnitEnums;
import com.yswy.common.exception.LoginException;
import com.yswy.common.exception.WxLoginException;
import com.yswy.common.utils.DateUtils;
import com.yswy.common.utils.HttpUtil;
import com.yswy.common.utils.StringUtils;
import com.yswy.common.utils.TokenTool;
import com.yswy.domain.entity.*;
import com.yswy.domain.mapper.AgreementOrderPayLogMapper;
import com.yswy.domain.mapper.UserWxOpenMapper;
import com.yswy.domain.mapper.WxProgramConfigMapper;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.yswy.domain.common.util.TokenUtils.getTokenToken;

@Service
@Slf4j
public class UserWxOpenServiceImpl extends ServiceImpl<UserWxOpenMapper, UserWxOpen>
        implements UserWxOpenService {
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private AppUserService appUserService;
    @Autowired
    private UserThirdPartyAccountService userThirdPartyAccountService;

    @Autowired
    private WxProgramConfigService wxProgramConfigService ;

    @Override
    public UserWxOpen wxOauth(String code) {

        WxProgramConfig wxProgramConfig = wxProgramConfigService.getWxProgramConfig();
        String appid =wxProgramConfig.getAppId();
        String secret =wxProgramConfig.getAppSecret();

        String access_token = HttpUtil.doGet(" https://api.weixin.qq.com/sns/jscode2session?" +
                "appid=" + appid
                + "&secret="+secret
                + "&js_code=" + code
                + "&grant_type=authorization_code");
        log.info("获取 access_token " + access_token);
        if (access_token.contains("errcode")){
            throw new WxLoginException("微信授权失败");
        }
        JSONObject access_tokenJson = JSONObject.fromObject(access_token);
        String sessionKey = access_tokenJson.getString("session_key"); ;
        String openid = access_tokenJson.getString("openid");
        return UserWxOpen.builder()
                .openid(openid)
                .sessionKey(sessionKey)
                .build();

    }




    public static void main(String[] args) {
//        WxProgramConfig wxProgramConfig = wxProgramConfigService.getWxProgramConfig();
        String appid ="wxe0ac08eb2db2f2c0";
        String secret ="eac4d44515cd08ff324603d423f1f38d";

        String access_token = HttpUtil.doGet(" https://api.weixin.qq.com/sns/jscode2session?" +
                "appid=" + appid
                + "&secret="+secret
                + "&js_code=" + "0b1Mknll2X5Udc4EhUml2GND351MknlI"
                + "&grant_type=authorization_code");
        log.info("获取 access_token " + access_token);
        if (access_token.contains("errcode")){
            throw new WxLoginException("微信授权失败");
        }
        JSONObject access_tokenJson = JSONObject.fromObject(access_token);


        String authInfo = HttpUtil.doGet("https://api.weixin.qq.com/sns/userinfo?" +
                "access_token=73_NtZLcpNCJDE7HRxtdu7x1NM30qW17Ojd1-A6wGfGzrb9CXncbEBxP8Tq4eaecVZN2v3tR4DK6Nt24JkAcA7dYeCST5lx5DwOP3Tt8GAJ5piy4eBPnqXR6Ha7WVoGYZeAEAQIJ"
                + "&openid="+access_tokenJson.getString("openid"));
        log.info("获取 authInfo " + authInfo);
        JSONObject authInfoJson = JSONObject.fromObject(authInfo);
    }




}
